/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.googlecode.charts4j.Color;
import com.googlecode.charts4j.GCharts;
import com.googlecode.charts4j.PieChart;
import com.googlecode.charts4j.Slice;
//import com.sun.xml.internal.messaging.saaj.soap.SAAJMetaFactoryImpl;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.transaction.UserTransaction;
import entity.Customer;
import java.util.List;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author cyhiew
 */
public class HelloService {

    public static String sayHello(String name) {
        return "Hello " + name + "!";
    }
    
    public static List getCustomerList() {
        
        EntityManager entityManager = Persistence.createEntityManagerFactory("lordwebappPU").createEntityManager();
        Query query = entityManager.createNamedQuery("Customer.findAll");
        List<Customer> resultList = query.getResultList();
        for (Customer c : resultList) {
          System.out.println(c.getName() + " (" + c.getCity() + ")" + "\n");
        }
            
        return resultList;

    }

    public static String getPieChart(String name) {
        
        Slice s1 = Slice.newSlice(15, Color.newColor("CACACA"), "Mac", "Mac");
        Slice s2 = Slice.newSlice(50, Color.newColor("DF7417"), "Window", "Window");
        Slice s3 = Slice.newSlice(25, Color.newColor("951800"), "Linux", "Linux");
        Slice s4 = Slice.newSlice(10, Color.newColor("01A1DB"), "Others", "Others");

        PieChart pieChart = GCharts.newPieChart(s1, s2, s3, s4);
        pieChart.setTitle("Google Pie Chart", Color.BLACK, 15);
        pieChart.setSize(720, 360);
        pieChart.setThreeD(true);
        return pieChart.toURLString();

    }

    public void persist(Object object) {
        /* Add this to the deployment descriptor of this module (e.g. web.xml, ejb-jar.xml):
         * <persistence-context-ref>
         * <persistence-context-ref-name>persistence/LogicalName</persistence-context-ref-name>
         * <persistence-unit-name>lordwebappPU</persistence-unit-name>
         * </persistence-context-ref>
         * <resource-ref>
         * <res-ref-name>UserTransaction</res-ref-name>
         * <res-type>javax.transaction.UserTransaction</res-type>
         * <res-auth>Container</res-auth>
         * </resource-ref> */
        try {
            Context ctx = new InitialContext();
            UserTransaction utx = (UserTransaction) ctx.lookup("java:comp/env/UserTransaction");
            utx.begin();
            EntityManager em = (EntityManager) ctx.lookup("java:comp/env/persistence/LogicalName");
            em.persist(object);
            utx.commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            throw new RuntimeException(e);
        }
    }
}
